//
// Created by wkm on 2024-04-20.
//

#ifndef AES128CHIPSYSTEMC_SCHEDULEKEY_H
#define AES128CHIPSYSTEMC_SCHEDULEKEY_H

#include "../core_module/CONSTANT.h"
#include "../ROM/sbox.h"


static const unsigned char Rcon[10] = {
        0x01, 0x02, 0x04, 0x08,
        0x10, 0x20, 0x40, 0x80,
        0x1b, 0x36
};

SC_MODULE(schedulekey) {
    sc_in<bool> reset, start, clk;
    sc_out<exe_state> done, can_read;
    sc_in<sc_uint<4>> get_round; //获取第几轮的密钥
    sc_in<sc_uint<DATA_SIZE>> init_key_1, init_key_2, init_key_3, init_key_4,
            init_key_5, init_key_6, init_key_7, init_key_8,
            init_key_9, init_key_10, init_key_11, init_key_12,
            init_key_13, init_key_14, init_key_15, init_key_16;
    sc_out<sc_uint<DATA_SIZE>> out_key_1, out_key_2, out_key_3, out_key_4,
            out_key_5, out_key_6, out_key_7, out_key_8,
            out_key_9, out_key_10, out_key_11, out_key_12,
            out_key_13, out_key_14, out_key_15, out_key_16;

    sc_signal<sc_uint<DATA_SIZE>> addr1, addr2, addr3, addr4, addr5, addr6, addr7, addr8, addr9, addr10, addr11, addr12, addr13, addr14, addr15, addr16;
    sc_signal<bool> en_sbox_read;
    sc_signal<exe_state> sbox_done;
    sc_signal<sc_uint<DATA_SIZE>> data1, data2, data3, data4, data5, data6, data7, data8, data9, data10, data11, data12, data13, data14, data15, data16;
    sc_signal<bool> sbox_state_can_exe;
    exe_state next_done;

    sc_uint<DATA_SIZE> temp_output[16 * 11];
    sc_uint<8> round_num;
    sc_uint<DATA_SIZE> a, b, c, d, temp;
    bool first;

    sbox *mybox;

    void prc_schedulekey();

    void prc_read();

    SC_CTOR(schedulekey) {
        mybox = new sbox("mymybox");
        mybox->clk(clk);
        mybox->addr1(addr1);
        mybox->addr2(addr2);
        mybox->addr3(addr3);
        mybox->addr4(addr4);
        mybox->addr5(addr5);
        mybox->addr6(addr6);
        mybox->addr7(addr7);
        mybox->addr8(addr8);
        mybox->addr9(addr9);
        mybox->addr10(addr10);
        mybox->addr11(addr11);
        mybox->addr12(addr12);
        mybox->addr13(addr13);
        mybox->addr14(addr14);
        mybox->addr15(addr15);
        mybox->addr16(addr16);
        mybox->en_read(en_sbox_read);
        mybox->done(sbox_done);
        mybox->out_data1(data1);
        mybox->out_data2(data2);
        mybox->out_data3(data3);
        mybox->out_data4(data4);
        mybox->out_data5(data5);
        mybox->out_data6(data6);
        mybox->out_data7(data7);
        mybox->out_data8(data8);
        mybox->out_data9(data9);
        mybox->out_data10(data10);
        mybox->out_data11(data11);
        mybox->out_data12(data12);
        mybox->out_data13(data13);
        mybox->out_data14(data14);
        mybox->out_data15(data15);
        mybox->out_data16(data16);

        SC_METHOD(prc_schedulekey);
        sensitive << clk.pos();


        SC_METHOD(prc_read);
        sensitive << clk.pos();

    }

    ~schedulekey() {
        delete mybox;
    }
};

#endif //AES128CHIPSYSTEMC_SCHEDULEKEY_H
